﻿using System;
using System.Web.Mvc;
using INSE6260.OnlineBanking.Model.Transaction;
using INSE6260.OnlineBanking.Web.MVC.Helpers;

namespace INSE6260.OnlineBanking.Web.MVC.Controllers
{
    public class PaymentHistoryController : Controller
    {
        private readonly ITransactionRepository _transactionRepository;
        public PaymentHistoryController(ITransactionRepository transactionRepository)
        {
            _transactionRepository = transactionRepository;
        }

        public ActionResult Index()
        {

            return View();
        }

        public ActionResult ShowHistory(string startDate, string endDate)
        {
            DateTime toDate;
            DateTime fromDate;
            if (String.IsNullOrEmpty(startDate))
                fromDate = DateTime.Now.AddDays(-DateTime.Now.Day);
            else if (!DateTime.TryParse(startDate, out fromDate))
                ModelState.AddModelError("", "From Date is invalid.");
            if (String.IsNullOrEmpty(endDate))
                toDate = DateTime.Now;
            else if(!DateTime.TryParse(endDate,out toDate))
                ModelState.AddModelError("","To Date is invalid.");
           if (!ModelState.IsValid)
                return PartialView("PaymentHistoryList",null);
            
           var model = _transactionRepository.GetPaymentHistory(UserProvider.ActiveClient.ClientId, fromDate, toDate);
            return PartialView("PaymentHistoryList", model);
        }
    }
}
